import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Product {
    private int id;
    private String name;
    private double price;

    public Product(int id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    // Getter方法
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "商品ID：" + id + "，名称：" + name + "，价格：" + price;
    }
}
class ConsoleShop {
    private ProductService productService;
    private Map<Integer, Integer> shoppingCart; // 键：商品ID，值：购买数量
    private Scanner scanner;

    public ConsoleShop() {
        productService = new ProductService();
        shoppingCart = new HashMap<>();
        scanner = new Scanner(System.in);
    }

    // 显示商品列表
    public void showProducts() {
        Map<Integer, Product> products = productService.getAllProducts();
        System.out.println("=== 商品列表 ===");
        for (Product p : products.values()) {
            System.out.println(p);
        }
    }

    // 添加商品到购物车
    public void addToCart() {
        showProducts();
        System.out.print("请输入要购买的商品ID：");
        int productId = scanner.nextInt();
        System.out.print("请输入购买数量：");
        int quantity = scanner.nextInt();

        Product product = productService.getProductById(productId);
        if (product != null) {
            shoppingCart.put(productId, shoppingCart.getOrDefault(productId, 0) + quantity);
            System.out.println("已将【" + product.getName() + "】x" + quantity + " 添加到购物车！");
        } else {
            System.out.println("商品ID不存在，请重新输入！");
        }
    }

    // 查看购物车
    public void showCart() {
        if (shoppingCart.isEmpty()) {
            System.out.println("购物车为空！");
            return;
        }
        System.out.println("=== 购物车 ===");
        double totalPrice = 0;
        for (Map.Entry<Integer, Integer> entry : shoppingCart.entrySet()) {
            Product product = productService.getProductById(entry.getKey());
            int quantity = entry.getValue();
            double subTotal = product.getPrice() * quantity;
            totalPrice += subTotal;
            System.out.println(product.getName() + " x" + quantity + "，单价：" + product.getPrice() + "，小计：" + subTotal);
        }
        System.out.println("总计：" + totalPrice);
    }

    // 主菜单
    public void showMenu() {
        while (true) {
            System.out.println("\n=== 控制台商城 ===");
            System.out.println("1. 查看商品列表");
            System.out.println("2. 添加商品到购物车");
            System.out.println("3. 查看购物车");
            System.out.println("4. 退出");
            System.out.print("请选择操作：");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    showProducts();
                    break;
                case 2:
                    addToCart();
                    break;
                case 3:
                    showCart();
                    break;
                case 4:
                    System.out.println("感谢使用，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    // main方法：程序入口
    public static void main(String[] args) {
        ConsoleShop shop = new ConsoleShop();
        shop.showMenu();
    }
}
class ProductService {
    private Map<Integer, Product> productMap;

    // 构造函数：初始化商品数据
    public ProductService() {
        productMap = new HashMap<>();
        // 初始商品
        productMap.put(1, new Product(1, "笔记本电脑", 5999.99));
        productMap.put(2, new Product(2, "智能手机", 3999.99));
        // 【动手修改】添加自己喜欢的商品
        productMap.put(3, new Product(3, "无线耳机", 899.00));
        productMap.put(4, new Product(4, "机械键盘", 499.00));
    }

    // 获取所有商品
    public Map<Integer, Product> getAllProducts() {
        return productMap;
    }

    // 根据ID查询商品
    public Product getProductById(int id) {
        return productMap.get(id);
    }
}